package TwentyOne.Class.Map;

/*
 * 复杂多对多映射(角色与权限)
 * 
 * 一个用户可以拥有多个角色，一个角色可能有多个用户
 * 一个角色可以拥有多个权限
 * 
 * 权限：权限ID、角色ID、名称
 * 角色：角色ID、名称
 * 用户：用户ID、姓名
 * 
 * 实现查询功能
 * 1.可以根据一个用户找到该用户对应的所有角色，以及每一个角色对应的所有权限信息；
 * 2.可以根据一个角色找到该角色下的所有权限，以及拥有此角色的全部用户信息；
 * 3.可以根据一个权限找到具备有此权限的所有用户信息
 */
class User {

    private String mid;
    private String name;
    private Role roles[];

    public User(String mid, String name) {
        this.mid = mid;
        this.name = name;
    }

    public void setRoles(Role roles[]) {
        this.roles = roles;
    }

    public Role[] getRoles() {
        return this.roles;
    }

    public String getInfo() {
        return "【用户信息】mid = " + this.mid + "、name = " + this.name;
    }
}

class Role {

    private long rid;
    private String title;
    private User users[];
    private Privilege privileges[];

    public Role(long rid, String title) {
        this.rid = rid;
        this.title = title;
    }

    public void setUsers(User users[]) {
        this.users = users;
    }

    public void setPrivileges(Privilege privileges[]) {
        this.privileges = privileges;
    }

    public User[] getUsers() {
        return this.users;
    }

    public Privilege[] getPrivileges() {
        return this.privileges;
    }

    public String getInfo() {
        return "【角色信息】rid = " + this.rid + "、title = " + this.title;
    }
}

class Privilege {

    private long pid;
    private String title;
    private Role roles[];

    public Privilege(long pid, String title) {
        this.pid = pid;
        this.title = title;
    }

    public void setRoles(Role roles[]) {
        this.roles = roles;
    }

    public Role[] getRoles() {
        return this.roles;
    }

    public String getInfo() {
        return "【权限信息】pid = " + this.pid + "、title = " + this.title;
    }
}

public class a12_MapManyToMany {

    public static void main(String[] args) {
        //根据结构设置对象数据
        User userA = new User("z1", "张三");
        User userB = new User("c1", "李四");
        Role roleA = new Role(1, "系统配置");
        Role roleB = new Role(2, "备份配置");
        Role roleC = new Role(3, "人事配置");
        Privilege priA = new Privilege(1000, "系统初始化");
        Privilege priB = new Privilege(1001, "系统还原");
        Privilege priC = new Privilege(1002, "系统环境修改");
        Privilege priD = new Privilege(2000, "备份员工数据");
        Privilege priE = new Privilege(2001, "备份部门数据");
        Privilege priF = new Privilege(2002, "备份公文数据");
        Privilege priG = new Privilege(3000, "增加员工");
        Privilege priH = new Privilege(3001, "编辑员工");
        Privilege priI = new Privilege(3002, "浏览员工");
        Privilege priJ = new Privilege(3003, "员工离职");
        //增加角色与权限的对应关系
        roleA.setPrivileges(new Privilege[]{priA, priB, priC});
        roleB.setPrivileges(new Privilege[]{priD, priE, priF});
        roleC.setPrivileges(new Privilege[]{priG, priH, priI, priJ});
        //增加权限与角色对应
        priA.setRoles(new Role[]{roleA});
        priB.setRoles(new Role[]{roleA});
        priC.setRoles(new Role[]{roleA});
        priD.setRoles(new Role[]{roleB});
        priE.setRoles(new Role[]{roleB});
        priF.setRoles(new Role[]{roleB});
        priG.setRoles(new Role[]{roleC});
        priH.setRoles(new Role[]{roleC});
        priI.setRoles(new Role[]{roleC});
        priJ.setRoles(new Role[]{roleC});
        //增加用户与角色的对应关系
        userA.setRoles(new Role[]{roleA, roleB});
        userB.setRoles(new Role[]{roleA, roleB, roleC});
        roleA.setUsers(new User[]{userA, userB});
        roleB.setUsers(new User[]{userA, userB});
        roleC.setUsers(new User[]{userB});
        //根据要求获取数据
        System.out.println("-------------通过用户查找角色信息--------------");
        System.out.println(userB.getInfo());
        for (int x = 0; x < userB.getRoles().length; x++) {
            System.out.println("\t|- " + userB.getRoles()[x].getInfo());
            // for (int y = 0; y < userB.getRoles()[x].getPrivileges().length; y++) {
            //     System.out.println("\t\t|- " + userB.getRoles()[x].getPrivileges()[y].getInfo());
            // }
        }
        System.out.println("-------------通过角色查找信息--------------");
        System.out.println(roleB.getInfo());
        System.out.println("\t|- 浏览此角色下的所有权限信息：");
        for (int x = 0; x < roleB.getPrivileges().length; x++) {
            System.out.println("\t\t|- " + roleB.getPrivileges()[x].getInfo());
        }
        System.out.println("\t|- 浏览此角色下的所有用户信息：");
        for (int x = 0; x < roleB.getUsers().length; x++) {
            System.out.println("\t\t|- " + roleB.getUsers()[x].getInfo());
        }
        System.out.println("-------------通过权限查找信息--------------");
        System.out.println(priA.getInfo());
        for (int x = 0; x < priA.getRoles().length; x++) {
            System.out.println("\t|- " + priA.getRoles()[x].getInfo());
            for (int y = 0; y < priA.getRoles()[x].getUsers().length; y++) {
                System.out.println("\t\t|- " + priA.getRoles()[x].getUsers()[y].getInfo());
            }
        }
    }
}
